import { colorful, generateID, unzipXandY } from "@/utils"
/* 未测试 */
/* 全局任务执行模块 */
export function globalMisson():void{
    if (!Memory.GlobalMisson) Memory.GlobalMisson = []
    DelayGlobalMisson()

    /* 执行全局任务 */
    G_TRV()
}

/* 全局任务添加函数 */
export function MountGlobalMisson(misson:GlobalMissonData):boolean{
    var tempID = Game.shard[-1] + generateID()
    misson.ID = tempID
    if (!misson.onlyOnce)
    {
        Memory.GlobalMisson.push(misson)
    }
    else
    {
        for (var i of Memory.GlobalMisson)
        {
            if (i.name == misson.name)
                return false
        }
        Memory.GlobalMisson.push(misson)
    }
    console.log(colorful(`${Game.shard.name}挂载全局任务${misson.name}成功!`,'green',true))
    return true
}

/* 全局任务删除函数 通过Name删除 */
export function DeleteGlobalMisson(missonName:string):boolean{
    for (var i of Memory.GlobalMisson)
    {
        if (i.name == missonName)
        {
            var index = Memory.GlobalMisson.indexOf(i)
            Memory.GlobalMisson.splice(index,1)
            return true
        }
    }
    return false
}

/* 全局任务删除 通过ID删除 */
export function DeleteGlobalMisson_ID(ID:string):boolean{
    for (var i of Memory.GlobalMisson)
    {
        if (i.ID == ID)
        {
            console.log(colorful(`${Game.shard.name}删除全局任务${i.name}成功!`,'yellow',true))
            var index = Memory.GlobalMisson.indexOf(i)
            Memory.GlobalMisson.splice(index,1)
            return true
        }
    }
    return false
}

/* 全局任务超时 */
export function DelayGlobalMisson():void{
    for (var i of Memory.GlobalMisson)
    {
        i.delayTick --
        if (i.delayTick <= 0)
            DeleteGlobalMisson_ID(i.ID)
    }
}

/* 获取全局任务 */
export function getGlobalMisson(missonName:string):GlobalMissonData | null{
    for (var i of Memory.GlobalMisson)
    {
        if (i.name == missonName && i.shard==-1?'sim':`shard${i.shard}` == Game.shard.name)
            return i
    }
    return null
}


/* 显示TowerRange可视化任务处理 */
export function G_TRV():void{
    let thisTask = getGlobalMisson('防御塔数据可视化')
    if (!thisTask) return
    if (!global.towerRangeData) global.towerRangeData = {}
    LoopTRV:
    for (var i of thisTask.disroom)
    {
        if (!global.towerRangeData[i])
            continue LoopTRV
        /* 有数据就进行可视化 */
        LoopV:
        for (var posData in global.towerRangeData[i])
        {
            /* 数据 */
            let posXY = unzipXandY(posData)
            if (!posData) continue LoopV
            let tx = posXY[0]
            let ty = posXY[1]
            var Data = global.towerRangeData[i][posData]
            if (!thisTask.type)
            {
                switch (Game.time){
                    case Game.time%2:{
                        new RoomVisual(i).text(`${Data.heal}`,tx,ty,{color:'greed',font:0.4})
                        break
                    }
                    case Game.time%3:{
                        new RoomVisual(i).text(`${Data.repair}`,tx,ty,{color:'yellow',font:0.4})
                        break
                    }
                    default:{
                        new RoomVisual(i).text(`${Data.attack}`,tx,ty,{color:'red',font:0.4})
                        break
                    }
                }
                
            }
            else
            {
                switch (thisTask.type){
                    case 'attack':{
                        new RoomVisual(i).text(`${Data.attack}`,tx,ty,{color:'red',font:0.4})
                        break
                    }
                    case 'heal':{
                        new RoomVisual(i).text(`${Data.repair}`,tx,ty,{color:'yellow',font:0.4})
                        break
                    }
                    case 'repair':{
                        new RoomVisual(i).text(`${Data.heal}`,tx,ty,{color:'green',font:0.4})
                        break
                    }
                }
            }
        }
    }
}